// pages/expectList/expectList.js
const app = getApp();
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    rows: 8,
    isOver: false,
    itemList: [],
    successSearchLoading: false,
    successSearchLoadingComplete: false,
    hadData: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "家长期待"
    })
    var activityId = options.activityId;
    this.setData({
      activityId: activityId
    })
    this.getUserInfo();
  },
  lower: function (e) {
    if (!this.data.isOver) {
      this.queryChildGrowthExpect();
    }
  },
  queryChildGrowthExpect: function() {
    var that = this;
    that.setData({
      isManager: app.globalData.appUser.teacherInfo && app.globalData.appUser.teacherInfo.isManager == 1 ? true : false
    })
    wx.request({
      url: app.httpUrl + '/activity/live/queryChildGrowthExpect',
      data: {
        activityId: that.data.activityId,
        page: this.data.page,
        rows: this.data.rows
      },
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        if (res.data.data.totalPage == 0) {
          that.setData({ hadData: false});
          return;
        }
        if (that.data.page >= res.data.data.totalPage) {
          that.setData({
            isOver: true,
            successSearchLoadingComplete: true
          })
        }
        that.data.page += 1;
        var list = res.data.data.results;
        for (var i in list) {
          list[i].createDate = util.formatDate(list[i].createTime, 'Y.M.D h:m');
          var relation = '';
          if (list[i].childRelation == 1) relation="宝贝的爸爸";
          else if (list[i].childRelation == 2) relation = "宝贝的妈妈";
          else if (list[i].childRelation == 3) relation = "宝贝的亲戚";
          else relation = "宝贝父母的朋友";
          list[i].parentDesc = list[i].childName + relation;
          that.data.itemList.push(list[i]);
        }
        that.setData({
          itemList: that.data.itemList,
          page: that.data.page
        });
      }
    })
  },
  choiceSet: function (event) {
    var that = this;
    var idx = event.currentTarget.dataset.idx;
    var list = that.data.itemList;
    list[idx].isChoice = list[idx].isChoice == 1 ? 0 : 1;
    wx.request({
      url: app.httpUrl + '/activity/live/updateChoiceParentMessage',
      data: {
        isChoice: list[idx].isChoice,
        id: list[idx].id
      },
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function (res) {
        if(res.data.code == '000') {
          that.setData({
            itemList: list
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  previewImageHead: function(event) {
    var src = event.currentTarget.dataset.src;
    var urls = [src]
    wx.previewImage({
      current: src, // 当前显示图片的http链接 
      urls: urls // 需要预览的图片http链接列表     
    })
  },
  previewImage: function (event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var idx = event.currentTarget.dataset.idx;
    var list = this.data.itemList[idx].attachList;
    var urls = [];
    for (var i in list) {
      urls.push(app.imgUrl + list[i].attachUrl);
    }
    wx.previewImage({
      current: src, // 当前显示图片的http链接 
      urls: urls // 需要预览的图片http链接列表     
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '@快乐 看看家长们的期待 From陶学趣',
      path: '/pages/expectList/expectList?activityId=' + this.data.activityId,
      success: function (res) {
        //转发成功
      }
    }
  },
  getUserInfo: function () {
    let that = this;
    if (app.globalData.appUser != null) {
      this.queryChildGrowthExpect();
      return;
    }
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.httpUrl + '/common/getUserInfo',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: "POST",
            success: function (res) {
              if (res.data.code == '000') {
                app.globalData.appUser = res.data.data;
                app.globalData.openid = res.data.data.openid;
                that.queryChildGrowthExpect();
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  }
})